home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / share / checkbox / scripts / keyboard_test < prev    next >
Encoding:
Text File  |  2009-04-27  |  1.7 KB  |  76 lines

  1. #!/usr/bin/python
  2.  
  3. import os
  4. import sys
  5.  
  6. def cli_prompt():
  7.     import termios
  8.  
  9.     limit = 50
  10.     separator = ord("\n")
  11.     fileno = sys.stdin.fileno()
  12.     saved_attributes = termios.tcgetattr(fileno)
  13.     attributes = termios.tcgetattr(fileno)
  14.     attributes[3] = attributes[3] & ~(termios.ICANON)
  15.     attributes[6][termios.VMIN] = 1
  16.     attributes[6][termios.VTIME] = 0
  17.     termios.tcsetattr(fileno, termios.TCSANOW, attributes)
  18.  
  19.     sys.stdout.write("Enter text:\n")
  20.  
  21.     input = ""
  22.     try:
  23.         while len(input) < limit:
  24.             ch = str(sys.stdin.read(1))
  25.             if ord(ch) == separator:
  26.                 break
  27.             input += ch
  28.     finally:
  29.         termios.tcsetattr(fileno, termios.TCSANOW, saved_attributes)
  30.  
  31. def gtk_prompt():
  32.     import pygtk
  33.     pygtk.require('2.0')
  34.     import gtk
  35.  
  36.     # create a new window
  37.     window = gtk.Window(gtk.WINDOW_TOPLEVEL)
  38.     window.set_size_request(200, 100)
  39.     window.set_resizable(False)
  40.     window.set_title("Type Text")
  41.     window.connect("delete_event", lambda w,e: gtk.main_quit())
  42.  
  43.     vbox = gtk.VBox(False, 0)
  44.     window.add(vbox)
  45.     vbox.show()
  46.  
  47.     entry = gtk.Entry()
  48.     entry.set_max_length(50)
  49.     vbox.pack_start(entry, True, True, 0)
  50.     entry.show()
  51.  
  52.     hbox = gtk.HBox(False, 0)
  53.     vbox.add(hbox)
  54.     hbox.show()
  55.  
  56.     button = gtk.Button(stock=gtk.STOCK_CLOSE)
  57.     button.connect("clicked", lambda w: gtk.main_quit())
  58.     vbox.pack_start(button, False, False, 0)
  59.     button.set_flags(gtk.CAN_DEFAULT)
  60.     button.grab_default()
  61.     button.show()
  62.     window.show()
  63.  
  64.     gtk.main()
  65.  
  66. def main(args):
  67.     if "DISPLAY" in os.environ:
  68.         gtk_prompt()
  69.     else:
  70.         cli_prompt()
  71.  
  72.     return 0
  73.  
  74. if __name__ == "__main__":
  75.     sys.exit(main(sys.argv[1:]))
  76.